﻿<%@ Page Language="C#" AutoEventWireup="true" Inherits="System.Web.UI.Page" %>

<%@ Import Namespace="Portal.Data.Entity" %>

<style>
    .crawlerUrl {
        position: absolute;
        bottom: 3px;
        right: 3px;
        padding: 0 3px;
        background-color: #ccc;
        text-align: center;
        cursor: pointer;
    }
</style>
<div class="bot-navigation">
    <div class="jumping">
        <div style="position: absolute; left: -320px; top: -15px; width: 290px;">
            Xóa hết bài tin từ chương<br />
            <input type="text" id="txtChapterDel" placeholder="Chương" style="width: 50px" />
            <input type="text" id="txtToChapterDel" placeholder="Đến chương" style="width: 80px" />
            <input type="button" id="btnRemoveAll" value=" Chấp nhận xóa " /><div id="msgdel" style="float: right; margin-top: 10px; color: blue; font-weight: bold;"></div>
        </div>
        Đến trang
        <select id="goPageTop" onchange="goPageBookAdmin(<%=BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <asp:placeholder runat="server" id="phPagingTop"></asp:placeholder>
</div>
<br class="clear" />
<asp:repeater id="Contents" runat="server">
    <HeaderTemplate>
        <table class="Contents" cellpadding="0" cellspacing="0">
            <tr class="Header" style="font-size: 13px;">
                <td style="width: 80px;">
                    Id
                </td>
                <td style="width: 150px;">
                    Sách
                </td>
                <td  style="width: 80px;">
                    Chương
                </td>
                <td>
                    Tiêu đề
                </td>
                <td style="width: 100px;">
                   <a href="javascript:reCrawlAll();">Fix tất cả</a>
                </td>
                <td style="width: 30px;">
                    Edit
                </td>
                <td style="width: 30px;">
                </td>
            </tr>
    </HeaderTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    <AlternatingItemTemplate>
        <tr class="AlterItem">
            <td>
                <%#Eval("Id")%>
            </td>
            <td style="position: relative">
                <%#GetBook(Eval("Book_ID"))%>
            </td>
            <td>
                <%#Eval("Chapter")%>
            </td>
            <td style="position: relative">
                <%#GetDisplayTitle(Eval("Title"), Eval("URL"))%>
                <input type="button" value="Delete" class="CommandDelete" onclick="onChapDelete('<%#Eval("ID")%>    ');"/>
                <div style="color: #666;font-style: italic;font-size: 80%;">
                    <%# ((DateTime) Eval("CreatedDate")).ToString("dd/MM/yyyy HH:mm")%>
                </div>
                <div title='Xem nội dung trang gốc' class='crawlerUrl' onclick="window.open('<%#Eval("CrawlUrl")%>','_blank');">+</div>
            </td>
            <td>
                <%#GetBadData((int)Eval("Id"),(bool)Eval("Broken")) %>
            </td>
            <td style="padding-left: 10px;">
                <input type="button" value="Edit" class="CommandEdit" onclick="onChapEdit('<%#Eval("ID")%>    ');"/>
            </td>
            <td style="padding-left: 10px;">
               
            </td>
        </tr>
    </AlternatingItemTemplate>
    <ItemTemplate>
        <tr class="Item">
            <td>
                <%#Eval("Id")%>
            </td>
            <td style="position: relative">
                <%#GetBook(Eval("Book_ID"))%>
            </td>
             <td>
                <%#Eval("Chapter")%>
            </td>
            <td style="position: relative">
                <%#GetDisplayTitle(Eval("Title"), Eval("URL"))%>
                <input type="button" value="Delete" class="CommandDelete" onclick="onChapDelete('<%#Eval("ID")%>    ');"/>
                <div style="color: #666;font-style: italic;font-size: 80%;">
                    <%# ((DateTime) Eval("CreatedDate")).ToString("dd/MM/yyyy HH:mm")%>
                </div>
                <div title='Xem nội dung trang gốc' class='crawlerUrl' onclick="window.open('<%#Eval("CrawlUrl")%>','_blank');">+</div>
            </td>
             <td>
                <%#GetBadData((int)Eval("Id"),(bool)Eval("Broken")) %>
            </td>
            <td style="padding-left: 10px;">
                <input type="button" value="Edit" class="CommandEdit" onclick="onChapEdit('<%#Eval("ID")%>    ');"/>
            </td>
            <td style="padding-left: 10px;">
                
            </td>
        </tr>
    </ItemTemplate>
</asp:repeater>
<div class="bot-navigation">
    <div class="jumping">
        Đến trang
        <select id="goPageBottom" onchange="goPageBookAdmin(<%=BookId %>,this.options[this.selectedIndex].value,'#bookchaps');">
        </select>
    </div>
    <asp:placeholder runat="server" id="phPagingBottom"></asp:placeholder>
</div>
<script type="text/javascript">
    var bid = <%=BookId %> ;
    var curPage = parseInt(<%=Request["page"] %>)||0;
    
    function onChapDelete(chapid) {
        if(confirmDelete()) {
            loading(true);
            $.ajax({
                url: '/Ajax.aspx',                
                data: {
                    p: 'Content',
                    act: 'delete',
                    cid: chapid.trim(),
                    bid: bid
                },
                success: function () {
                    loading(false);
                    goPageBookAdmin(bid, curPage, '#bookchaps'); //reload
                    //remove overlay
                    $('#jquery-msg-overlay').remove();
                },
                error: function (data) {loading(false); alert(data); }
            });            
        }
    }
    function reCrawlAll() {
        $("a").each(function() {
            var id = $(this).attr('id');
            if (typeof id !='undefined' && id.indexOf("rcd") >= 0) {
                var cid = id.replace("rcd","");
                reCrawlData(cid);
            }
        });
    }
    jQuery(function ($) {
        $('#btnRemoveAll').click(function() {
            $('#msgdel').html('');
            var chapter = parseInt($('#txtChapterDel').val())||0;
            var tochapter = parseInt($('#txtToChapterDel').val())||0;
            if(chapter==0) {
                $('#txtChapterDel').focus();
                alert("Hãy điền số chương cần xóa!");
                return;
            }
            if(tochapter>0 && chapter>tochapter) {
                $('#txtToChapterDel').focus();
                return;
            }
            loading(true,'#msgdel');
            $.ajax({
                url: '/Ajax.aspx',
                data: {
                    p: 'Content',
                    act: 'removeFromChapter',
                    bid: '<%=BookId %>',
                    chapter:chapter,
                    tochapter:tochapter
                },
                success: function (data) {
                    loading(false,'#msgdel');
                    $('#msgdel').html(data);
                },
                error: function () { loading(false,'#msgdel'); }
            });
        });
        //bind to select page
        var maxpage = <%=maxpage%>;                
        for (var i = 1; i <= maxpage; i++) {
            var item = '<option value="'+i+'">'+i+'</option>';
            if(curPage==i)
                item = '<option selected="selected" value="'+i+'">'+i+'</option>';
            $('#goPageTop').append(item);
            $('#goPageBottom').append(item);
        }
        
        $('#txtChapter').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
        $('#txtChapterTop').keyup(function (e) {
            var key = e.keyCode || e.which;
            if (key === 13) {
                goChap(bid,$(this).val());
            }
        });
    });
    function reCrawlData(cid) {
        var holder = '#rcd' + cid;
        jQuery(holder).html("");
        loading(true, holder);
        jQuery.ajax({
            url: '/Ajax.aspx',
            data: {
                p: 'Content',
                act: 'reCrawlData',
                cid: cid
            },
            success: function (data) {
                loading(false,holder);
                jQuery(holder).html(data);
            },
            error: function () { loading(false,holder); }
        });
    }
</script>
<script runat="server">
    public static readonly int MAX50 = 50;
    public int BookId, maxpage;
    protected override void OnInit(EventArgs e)
    {
        //        if (!Portal.Data.Security.SecurityProvider.AllowCall())
        //            return;
        if (Portal.Data.PortalContext.CurrentUser == null)
            return;
        int.TryParse(Request["bid"], out BookId);
        int page, pagesize;
        int.TryParse(Request["page"], out page);
        if (BookId <= 0) return;

        int.TryParse(Request["pagesize"], out pagesize);
        if (pagesize < 0) pagesize = 1;
        if (pagesize == 0 || pagesize > MAX50) pagesize = MAX50;

        int count;
        var virtalPage = page - 1;
        if (virtalPage < 0) virtalPage = 0;
        var books = Portal.Data.Entity.Content.Paging(BookId, pagesize, virtalPage, out count);
        maxpage = count / pagesize;
        if (count % pagesize != 0)
            maxpage += 1;

        if (books.Count == 0)
        {
            Response.Write("");
            Response.End();
        }
        else
        {
            Contents.DataSource = books;
            Contents.DataBind();
            Common.AddPagingToolAjax(new Control[] { phPagingTop, phPagingBottom }, count, pagesize, "#bookchaps");
        }
    }

    protected string GetUser(object eval)
    {
        PortalUser user = PortalUser.FindByPrimaryKey(eval);
        return (user != null) ? user.Name : "";
    }

    protected string GetBook(object bookId)
    {
        var book = Book.FindByPrimaryKey(bookId);
        return (book != null) ? string.Format("<a target=_blank href=\"{0}\">{1}</a><div title='Xem nội dung trang gốc' class='crawlerUrl' onclick=\"window.open('{2}','_blank');\">+</div>", book.Url, book.FullName, book.CrawlUrl) : "";
    }
    protected string GetDisplayTitle(object title, object url)
    {
        return string.Format("<a href='{0}' target=_blank>{1}</a>", url, title);
    }
    protected string GetBadData(int cid, bool isBad)
    {
        if (isBad)
            return string.Format("<a title='Click để tự lấy lại nội dung!' id='rcd{0}' href='javascript:reCrawlData({0})' style='color:red'>Hỏng</a>", cid);
        return "";
    }
</script>
